package at.tugraz.akm.forume.webservice;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import at.tugraz.akm.forume.bean.Reply;
import at.tugraz.akm.forume.bean.Thread;
import at.tugraz.akm.forume.extras.JSONUtils;
import at.tugraz.akm.forume.extras.MySharedPreferences;
import at.tugraz.akm.forume.extras.Utils;

public abstract class Client {

	public static boolean checkUser(String user) {
		String url = "http://nibble.li/forume/user.php?action=check&username="
				+ user;
		return JSONUtils.getBoolean(url, "check");
	}

	public static boolean logIn(String username, String password) {
		password = Utils.md5(password);
		String url = "http://nibble.li/forume/user.php?action=login&username="
				+ username + "&password=" + password;
		return JSONUtils.getBoolean(url, "check");
	}

	public static boolean subscribed(Context context, int thread_id) {
		String url = "http://nibble.li/forume/subs.php?action=subscribed&user_id="
				+ MySharedPreferences.getString(context,
						MySharedPreferences.USER_ID)
				+ "&thread_id="
				+ thread_id;
		return JSONUtils.getBoolean(url, "check");
	}

	public static boolean subs(Context context, String url_thread) {
		String url = "http://nibble.li/forume/subs.php?action=new"
				+ "&user_id="
				+ MySharedPreferences.getString(context,
						MySharedPreferences.USER_ID)
				+ "&password="
				+ Utils.md5(MySharedPreferences.getString(context,
						MySharedPreferences.PASSWORD)) + "&" + "thread_url="
				+ url_thread.trim().toLowerCase();
		// Log.e("URL SUBS", url);
		return JSONUtils.getBoolean(url, "insert");
	}

	public static boolean newComment(Context context, String title,
			String content, int public1) {
		String url = "http://nibble.li/forume/create.php?action=new&"
				+ "user_id="
				+ MySharedPreferences.getString(context,
						MySharedPreferences.USER_ID)
				+ "&password="
				+ Utils.md5(MySharedPreferences.getString(context,
						MySharedPreferences.PASSWORD)) + "&" + "title="
				+ Utils.convertURL(title) + "&" + "content="
				+ Utils.convertURL(content) + "&" + "public=" + public1;
		Log.e("URL SUBS", url);
		return JSONUtils.getBoolean(url, "insert");
	}

	public static boolean newUser(Context context, String username,
			String email, String password) {
		String url = "http://nibble.li/forume/user.php?action=new&"
				+ "username=" + username + "&password=" + Utils.md5(password)
				+ "&email=" + email;
		Log.e("New User", url);
		return JSONUtils.getBoolean(url, "insert");
	}

	public static boolean comment(Context context, int thread_id, String content) {
		String password = Utils.md5(MySharedPreferences.getString(context,
				MySharedPreferences.PASSWORD));
		content = Utils.convertURL(content);
		String url = "http://nibble.li/forume/reply.php?action=new&user_id="
				+ MySharedPreferences.getString(context,
						MySharedPreferences.USER_ID) + "&password=" + password
				+ "&content=" + content + "&thread_id=" + thread_id;
		// Log.e("URL send comment", url);
		return JSONUtils.getBoolean(url, "insert");
	}

	public static Thread getThread(Context context, int thread_id) {
		Thread t = new Thread();
		JSONObject j = JSONUtils
				.getJSONObject("http://nibble.li/forume/thread.php?thread_id="
						+ thread_id);
		try {
			t.setId(j.getInt("id"));
			t.setTitle(j.getString("title"));
			t.setContent(j.getString("content"));
			String date_s = j.getString("date");
			SimpleDateFormat format_date = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			Date date = null;
			try {
				date = format_date.parse(date_s);
			} catch (ParseException ex) {
				date = new Date();
			}
			t.setDate(date);
			t.setModified(j.getInt("modified") == 1 ? true : false);
			t.setPublic1(j.getInt("public") == 1 ? true : false);
			t.setUrl(j.getString("url"));
			t.setUser(j.getString("username"));

			ArrayList<Reply> replies = new ArrayList<Reply>();

			if (!j.getString("replies").equalsIgnoreCase("null")) {

				JSONArray json_replies = j.getJSONArray("replies");
				for (int i = 0; i < json_replies.length(); i++) {
					Reply r = new Reply();

					JSONObject reply_object = json_replies.getJSONObject(i);

					r.setId(reply_object.getInt("id"));
					r.setUser(reply_object.getString("username"));
					r.setContent(reply_object.getString("content"));
					String date_s2 = reply_object.getString("date");
					Date date2 = null;
					try {
						date2 = format_date.parse(date_s2);
					} catch (ParseException ex) {
						date2 = new Date();
					}
					SimpleDateFormat format_out = new SimpleDateFormat(
							"dd-MM-yyy HH:mm");
					r.setDate(format_out.format(date2));
					replies.add(r);
				}
			}
			t.setReplyList(replies);

		} catch (JSONException e) {
			Log.e("getThread(context, " + thread_id + ")",
					e.getLocalizedMessage());
		}

		if (t.getReplyList() == null) {
			t.setReplyList(new ArrayList<Reply>());
		}

		return t;
	}

	public static ArrayList<Thread> search(Context context, String query) {
		return parseListThreads(context,
				"http://nibble.li/forume/threads.php?action=search&query="
						+ Utils.convertURL(query));
	}

	public static ArrayList<Thread> getThreadsSusbcribed(Context context) {
		return parseListThreads(
				context,
				"http://nibble.li/forume/threads.php?action=get_subs&user_id="
						+ MySharedPreferences.getString(context,
								MySharedPreferences.USER_ID));
	}

	public static ArrayList<Thread> parseListThreads(Context context, String url) {
		ArrayList<Thread> ts = new ArrayList<Thread>();

		JSONObject j = JSONUtils.getJSONObject(url);
		try {
			if (j != null) {
				JSONArray results = j.getJSONArray("results");
				for (int i = 0; i < results.length(); i++) {
					JSONObject result_object = results.getJSONObject(i);
					Thread t = new Thread();
					t.setId(result_object.getInt("id"));
					t.setTitle(result_object.getString("title"));
					t.setContent(result_object.getString("content"));
					String date_s = result_object.getString("date");
					SimpleDateFormat format_date = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					Date date = null;
					try {
						date = format_date.parse(date_s);
					} catch (ParseException ex) {
						date = new Date();
					}
					t.setDate(date);
					t.setPublic1(result_object.getInt("public") == 1 ? true
							: false);
					t.setModified(result_object.getInt("modified") == 1 ? true
							: false);
					int status_id = result_object.getInt("status_id");
					t.setStatusId(status_id);
					t.setUser(results.getJSONObject(i).getString("username"));
					t.setUrl(results.getJSONObject(i).getString("url"));
					try {
						t.setNum_replies(result_object.getInt("replies"));
					} catch (JSONException e) {
						t.setNum_replies(-1);
					}
					if (t.getUrl() == null) {
						t.setUrl("null");
					}
					ts.add(t);
				}
			}
		} catch (JSONException e) {
			Log.e("getThreadsSusbcribed(Context context)",
					e.getLocalizedMessage());
		}

		return ts;
	}
}
